package cn.dtsmusic.plugin;

import com.google.common.collect.Maps;

import cn.dtsmusic.test.ApplicationTests;
import cn.dtsmusic.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 全局异常处理
 *
 * @author leobelive
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	private static Log log = LogFactory.getLog(GlobalExceptionHandler.class );
	
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<?> handleRuntimeException(HttpServletRequest request, RuntimeException e) {
        printStackTrace(request, e);
        return ResponseEntity.badRequest().body(e.getMessage());
    }

    /**
     * 打印异常信息
     */
    private void printErrorMsg(Throwable e) {
//        log.error("handle:{}, msg:{}", e.getClass().getName(), e.getMessage());
        log.error("handle:{}, msg:{}");
    }

    /**
     * 打印异常信息和入参
     */
    private void printErrorMsg(HttpServletRequest request, Throwable e) {
        Map<String, String[]> params = request.getParameterMap();
        Map<String, Object> map = Maps.transformValues(
                params, arr -> arr.length == 0 ? null : arr.length == 1 ? arr[0] : arr
        );
//        log.error("handle:{}, msg:{}, params:{}", e.getClass().getName(), e.getMessage(), JsonUtils.toJson(map));
        log.error("handle:{}, msg:{}, params:{}");
    }

    /**
     * 打印堆栈和入参
     */
    private void printStackTrace(HttpServletRequest request, Throwable e) {
        Map<String, String[]> params = request.getParameterMap();
        Map<String, Object> map = Maps.transformValues(
                params, arr -> arr.length == 0 ? null : arr.length == 1 ? arr[0] : arr
        );
//        log.error("handle:{}, params:{}", e.getClass().getName(), JsonUtils.toJson(map), e);
        log.error("handle:{}, params:{}");
    }

}